package org.zjx.strategy.impl;

import org.zjx.command.ICommand;
import org.zjx.command.CompositeCommand;
import org.zjx.command.basic.ClickCommand;
import org.zjx.command.basic.WaitCommand;
import org.zjx.core.GameWindow;
import org.zjx.strategy.IGameStrategy;
import java.awt.image.BufferedImage;

/**
 * 战斗策略
 */
public class CombatStrategy implements IGameStrategy {
    @Override
    public ICommand analyze(GameWindow window, BufferedImage screenshot) {
        return new CompositeCommand(window, "CombatSequence") {
            @Override
            protected void initializeSubCommands() {
                // 战斗指令序列
                addCommand(new ClickCommand(window, 100, 200)); // 攻击
                addCommand(new WaitCommand(window, 800));
                addCommand(new ClickCommand(window, 300, 150)); // 技能1
                addCommand(new WaitCommand(window, 500));
                addCommand(new ClickCommand(window, 400, 150)); // 技能2
            }
        };
    }

    @Override
    public String getStrategyName() {
        return "CombatStrategy";
    }

    @Override
    public int getPriority() {
        return 1; // 高优先级
    }

    @Override
    public boolean isApplicable(GameWindow window, BufferedImage screenshot) {
        // 检测红色（战斗状态）
        return hasColor(screenshot, 255, 0, 0, 50);
    }

    private boolean hasColor(BufferedImage image, int r, int g, int b, int tolerance) {
        // 简化的颜色检测
        for (int y = 0; y < image.getHeight(); y += 10) {
            for (int x = 0; x < image.getWidth(); x += 10) {
                int rgb = image.getRGB(x, y);
                int red = (rgb >> 16) & 0xFF;
                int green = (rgb >> 8) & 0xFF;
                int blue = rgb & 0xFF;

                if (Math.abs(red - r) < tolerance &&
                        Math.abs(green - g) < tolerance &&
                        Math.abs(blue - b) < tolerance) {
                    return true;
                }
            }
        }
        return false;
    }
}